import { message } from "antd";
import { useEffect, useState } from "react";
import { reqExamination } from "../api/api";

export default function useExamination (role,setLoading){
  const [data,setData] = useState();
  console.log(role);

  const getExamination = (role) => {
    console.log("执行了");
    setLoading(true)
    reqExamination(role).then(
      res => {
        if (res.status === 200) {
          setData(ProcessTime(res.data))
        }
        if (res.status === 201) {
          setData(null)
          // message.error("没有相应的考试信息")
        }
        setLoading(false)
      },
      rej => { }
    )
  }

  useEffect(() => {
    if (role !== null) {
      getExamination(role)
    }
  }, [role])

  // 考试列表时间处理
  const ProcessTime = (data) => {
    return data?.map((item) => {
      return ({
        ...item,
        fd_endTime: newDate(item.fd_endTime),
        fd_startTime: newDate(item.fd_startTime)
      })
    })
  }
  // 时间处理
  const newDate = (key) => {
    const date = new Date(key);// 创建一个Date对象，并传入时间字符串
    const year = date.getFullYear(); // 年
    const month = String(date.getMonth() + 1).padStart(2, "0"); //月
    const day = String(date.getDate()).padStart(2, "0"); //日
    const hours = String(date.getHours()).padStart(2, "0"); //时
    const minutes = String(date.getMinutes()).padStart(2, "0"); //分
    // const seconds = String(date.getSeconds()).padStart(2, "0"); //秒
    return `${year}-${month}-${day} ${hours}:${minutes}`;
  }


  return {data,setData,getExamination,ProcessTime}

}